package com.alibaba.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : hanyang
 * @date : 2023-10-29 23:11
 **/

public class BeanCopyUtils {

    private BeanCopyUtils() {
    }

    /**
     * 单实体类的拷贝
     */
    public static <V> V copyBean(Object source, Class<V> clazz) {
        // 创建目标对象
        V result;
        try {
            result = clazz.newInstance();
            // 实现属性拷贝
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //返回结果
        return result;
    }

    /**
     * List的拷贝
     */
    public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }

}
